package com.smartwebee.android.blespp.hospital;

import android.databinding.DataBindingUtil;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.smartwebee.android.blespp.R;
import com.smartwebee.android.blespp.databinding.ActivityHospitalMainBinding;
import com.smartwebee.android.blespp.eventbusclass.AddPlanEvent;
import com.smartwebee.android.blespp.eventbusclass.GoToViewPagerItem;
import com.smartwebee.android.blespp.hospital.adapter.FragmentAdapter;
import com.smartwebee.android.blespp.hospital.fragment.ExerciseFragment;
import com.smartwebee.android.blespp.hospital.fragment.MessageFragment;
import com.smartwebee.android.blespp.hospital.fragment.MineFragment;
import com.smartwebee.android.blespp.hospital.fragment.SelfTestFragment;
import com.smartwebee.android.blespp.hospital.fragment.SurveyFragment;
import com.smartwebee.android.blespp.utils.Utils;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class HospitalMainActivity extends BaseActivity implements View.OnClickListener {
    ActivityHospitalMainBinding binding;
    List<TextView> tabTv;
    List<ImageView> tabImg;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_hospital_main);
        initView();
    }

    private void initView() {
        tabTv = Arrays.asList(binding.tv1, binding.tv2, binding.tv3, binding.tv4, binding.tv5);
        tabImg = Arrays.asList(binding.imgTab1, binding.imgTab2, binding.imgTab3, binding.imgTab4, binding.imgTab5);
        List<Fragment> fragments = new ArrayList<>();
        fragments.add(new ExerciseFragment());
        fragments.add(new SelfTestFragment());
        fragments.add(new SurveyFragment());
        fragments.add(new MessageFragment());
        fragments.add(new MineFragment());
        FragmentAdapter adapter = new FragmentAdapter(getSupportFragmentManager(), fragments);
        binding.viewpager.setAdapter(adapter);
        initViewpager();
    }

    private void initViewpager() {
        binding.viewpager.setCurrentItem(2);
        binding.clayout1.setOnClickListener(this);
        binding.clayout2.setOnClickListener(this);
        binding.clayout3.setOnClickListener(this);
        binding.clayout4.setOnClickListener(this);
        binding.clayout5.setOnClickListener(this);
        binding.viewpager.setOffscreenPageLimit(5);
        binding.viewpager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                for (int i = 0; i < tabTv.size(); i++) {
                    if (i == position) {
                        tabTv.get(i).setTextColor(getResources().getColor(R.color.red));
                    } else {
                        tabTv.get(i).setTextColor(getResources().getColor(R.color.black));
                    }
                }
                switch (position) {
                    case 0:
                        binding.imgTab1.setImageResource(R.drawable.tabbar_01_on);
                        binding.imgTab2.setImageResource(R.drawable.tabbar_02);
                        binding.imgTab3.setImageResource(R.drawable.tabbar_03);
                        binding.imgTab4.setImageResource(R.drawable.tabbar_04);
                        binding.imgTab5.setImageResource(R.drawable.tabbar_04);
                        break;
                    case 1:
                        binding.imgTab1.setImageResource(R.drawable.tabbar_01);
                        binding.imgTab2.setImageResource(R.drawable.tabbar_02_on);
                        binding.imgTab3.setImageResource(R.drawable.tabbar_03);
                        binding.imgTab4.setImageResource(R.drawable.tabbar_04);
                        binding.imgTab5.setImageResource(R.drawable.tabbar_04);
                        break;
                    case 2:
                        binding.imgTab1.setImageResource(R.drawable.tabbar_01);
                        binding.imgTab2.setImageResource(R.drawable.tabbar_02);
                        binding.imgTab3.setImageResource(R.drawable.tabbar_03_on);
                        binding.imgTab4.setImageResource(R.drawable.tabbar_04);
                        binding.imgTab5.setImageResource(R.drawable.tabbar_04);
                        break;
                    case 3:
                        binding.imgTab1.setImageResource(R.drawable.tabbar_01);
                        binding.imgTab2.setImageResource(R.drawable.tabbar_02);
                        binding.imgTab3.setImageResource(R.drawable.tabbar_03);
                        binding.imgTab4.setImageResource(R.drawable.tabbar_04_on);
                        binding.imgTab5.setImageResource(R.drawable.tabbar_04);
                        break;
                    case 4:
                        binding.imgTab1.setImageResource(R.drawable.tabbar_01);
                        binding.imgTab2.setImageResource(R.drawable.tabbar_02);
                        binding.imgTab3.setImageResource(R.drawable.tabbar_03);
                        binding.imgTab4.setImageResource(R.drawable.tabbar_04);
                        binding.imgTab5.setImageResource(R.drawable.tabbar_04_on);
                        break;

                }

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.clayout1:
                binding.viewpager.setCurrentItem(0);
                break;
            case R.id.clayout2:
                binding.viewpager.setCurrentItem(1);
                break;
            case R.id.clayout3:
                binding.viewpager.setCurrentItem(2);
                break;
            case R.id.clayout4:
                binding.viewpager.setCurrentItem(3);
                break;
            case R.id.clayout5:
                binding.viewpager.setCurrentItem(4);
                break;
        }
    }

    @Override
    protected boolean isUseEventBus() {
        return true;
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(GoToViewPagerItem event) {
        binding.viewpager.setCurrentItem(event.getPosition());
    }
}
